package armserver.deploy;

import java.io.File;
import java.io.FilenameFilter;
import java.util.HashMap;
import java.util.Map;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistration;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import armserver.config.ArmServerConfig;


public class ArmDeploy implements ArmDeployMBean,MBeanRegistration{
	
	private static final Logger log = Logger.getLogger(ArmDeploy.class);
	private MBeanServer server;
	public static String deployName="ArmServer:service=ArmDeploy";
	private static  Map deployRespository = new HashMap();
	private  String deployDir=ArmServerConfig.SERVERDEPLOYDIR;
	private String[] xmls;
	private ApplicationContext act;
	private String domain = "ArmServer.deploy:name=";
	
	private void init()
	{
		this.loadDeployFile();
		if(xmls==null||xmls.length<1)
		{
			log.error("not found deployment files");
			return;
		}
		for (int i = 0; i < xmls.length; i++) {
			xmls[i] = "classpath:" + xmls[i];
		}
		act = new FileSystemXmlApplicationContext(xmls);
	}

	public ObjectName preRegister(MBeanServer server, ObjectName name)
			throws Exception {
		this.server = server;
		return name;
	}

	public void postRegister(Boolean registrationDone) {
		this.init();
		if(act==null)
		{
			log.info("deployment files is not found deploy MBean");
			return;
		}
		String[] beanNames = act.getBeanDefinitionNames();
	    for(String beanName:beanNames)
	    {
			if (beanName.endsWith("MBean")) {
				Object bean = act.getBean(beanName);
				try {
					server.registerMBean(bean,
							new ObjectName(domain + beanName));
					deployRespository.put(beanName, bean);
					log.info(beanName + " registry to MBeanServer");
				} catch (InstanceAlreadyExistsException e) {
					e.printStackTrace();
				} catch (MBeanRegistrationException e) {
					e.printStackTrace();
				} catch (NotCompliantMBeanException e) {
					e.printStackTrace();
				} catch (MalformedObjectNameException e) {
					e.printStackTrace();
				} catch (NullPointerException e) {
					e.printStackTrace();
				}
			}
	    }
	}

	public void preDeregister() throws Exception {
		
	}

	public void postDeregister() {
		
	}
	
	private void loadDeployFile()
	{
		try{
			log.info("load deploy dir:"+this.deployDir);
			File deployFile = new File(this.deployDir);
			if(!deployFile.exists())
			{
				throw new Exception(deployDir+" not exist");
			}
			if(deployFile.exists())
			{
				xmls = deployFile.list(new DeployFileFilter());
			}
		}
		catch(Exception e)
		{
			log.error("load deploy files Exception,may be reason:"+e.getMessage()+" not load");
		}
	}
	
	static class DeployFileFilter implements FilenameFilter{


		public boolean accept(File dir, String name) {
			if(name.endsWith(".xml"))
			{
				log.info("load deploy xml file name:"+name);
				return true;
			}
			return false;
		}
		
	}

	public String getDeployDir() {
		return deployDir;
	}

	public void setDeployDir(String deployDir) {
		this.deployDir = deployDir;
	}
	

}
